package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemPostMapper;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private SystemPostMapper systemPostMapper;

    @Override
    public void queryPageUser(Page<SystemUserItemVo> systemUserItemVoPage, SystemUserQueryVo queryVo) {
        systemUserMapper.queryPageUser(systemUserItemVoPage, queryVo);
    }

    @Override
    public SystemUserItemVo queryUserById(Long id) {
        SystemUser userById = getById(id);
        userById.setPassword(null);
        if (userById == null) {
            throw new LeaseException(ResultCodeEnum.FAIL);
        }
        SystemPost systemPost = systemPostMapper.selectById(userById.getPostId());

        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        BeanUtils.copyProperties(userById,systemUserItemVo);
//        systemUserItemVo.setPassword(null);
        systemUserItemVo.setPostName(systemPost.getName());

        return systemUserItemVo;
    }

    @Override
    public void saveOrUpdateUserInfo(SystemUser systemUser) {
        LambdaQueryWrapper<SystemUser> systemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //保存或修改查询数据库是否有username  SELECT COUNT(*) FROM SYSTEM_USER WHERE username = "xxxx" 检查账号问题（不能重复） 保存-》都不能重复
        systemUserLambdaQueryWrapper.eq(SystemUser::getUsername,systemUser.getUsername());
        //有id 更新  去除id行判断 SELECT COUNT(*) FROM SYSTEM_USER WHERE username = "xxxx" AND id != 1; 更新-》可以跟原有的重复，不能跟别的重复
        systemUserLambdaQueryWrapper.ne(!ObjectUtils.isEmpty(systemUser.getId()),SystemUser::getId,systemUser.getId());
        //count 是否有条件值 有值即为有名
        long count = count(systemUserLambdaQueryWrapper);
        // 有值即为有名 抛出账号存在异常
        if (count>0){
            throw  new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        //无值 密码加密 保存更新
        if(!ObjectUtils.isEmpty(systemUser.getPassword())){
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        saveOrUpdate(systemUser);
    }
}




